'use strict';

Object.defineProperty(exports, "__esModule", {
	value: true
});
exports.carry = carry;
exports.scientific = scientific;
/**
 * 数字保留小数位
 * @param {*} num 
 * @param {*} digit 保留的位数，默认0
 * @param {*} method 进位方式
 */
function carry(num) {
	var digit = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
	var method = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : Math.round;

	var f = parseFloat(num);
	return method(f * Math.pow(10, digit)) / Math.pow(10, digit);
}

/**
 * 数值使用英文单位缩写
 * @param {*} num 
 * @param {*} digit 保留小数
 * @param {*} method 
 * @returns 
 */
function scientific(num) {
	var digit = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
	var method = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : Math.round;

	var units = ["", 'K', 'M', 'B', 'T', 'P', 'E', 'Z', 'Y', 'S', 'L', 'X', 'D'];
	var n = String(num);
	var length = n.indexOf('.');
	if (length == -1) {
		length = n.length;
	}
	var d = length / 3;
	var index = String(d).indexOf('.') == -1 ? d - 1 : Math.floor(d);
	return carry(num / Math.pow(1000, index), digit, method) + units[index];
}

exports.default = {
	carry: carry,
	scientific: scientific
};